package com.ws.universal.tools.utils;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import kotlin.jvm.internal.Intrinsics;

/**
 * 平均分配
 * GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
 * binding.rcyData.addItemDecoration(new SpacesItemDecortion2(15, 2));
 * binding.rcyData.setLayoutManager(gridLayoutManager);
 * binding.rcyData.setAdapter(mostFavoriteAdapter);
 */
public class SpacesItemDecortion extends RecyclerView.ItemDecoration {
    private int space;
    private int column;

    public SpacesItemDecortion(int space, int column) {
        this.space = DisplayUtils.dipTopx(space);
        this.column = column;
    }

    public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
        Intrinsics.checkNotNullParameter(outRect, "outRect");
        Intrinsics.checkNotNullParameter(view, "view");
        Intrinsics.checkNotNullParameter(parent, "parent");
        Intrinsics.checkNotNullParameter(state, "state");
        int position = parent.getChildAdapterPosition(view);
        int spanSpace = this.space * (this.column + 1) / this.column;
        int colIndex = position % this.column;
        outRect.left = this.space * (colIndex + 1) - spanSpace * colIndex;
        outRect.right = spanSpace * (colIndex + 1) - this.space * (colIndex + 1);
        if (position >= this.column) {
            outRect.top = this.space;
        }

    }
}
